{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import sys\n",
    "import os\n",
    "import tensorflow as tf\n",
    "import numpy as np\n",
    "from mtcnn.mtcnn import mtcnn\n",
    "from batch_processing import batch_processing\n",
    "import cv2\n",
    "import time"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "class mtcnn_batchprocess():\n",
    "    def __init__(self):\n",
    "        self.m_mtcnn = mtcnn()\n",
    "        self.batch_processing = batch_processing()\n",
    "        print 'mtcnn_batchprocess Init()!'\n",
    "\n",
    "    def get_file_list(self, file_path, file_type = '.jpg'):\n",
    "        path_list, file_list = self.batch_processing.get_file_list(file_path, file_type)\n",
    "        return path_list, file_list\n",
    "    \n",
    "    def mtcnn_process(path_list, file_list, margin_value = 0, margin_ratio = 0.0,\n",
    "                      resize_type = 0, resize_ratio = 1.0, controled_size = 0,\n",
    "                      base_folder = '', save_folder = ''):\n",
    "\n",
    "#         margin_ratio > 0.0:  margin at ratio (margin_ratio)\n",
    "#         margin_ratio = 0.0:  margin = margin_value              \n",
    "\n",
    "#        if base_path != '' and save_folder_name != '' : save file to other path(save_path)\n",
    "#             such as: image_path  = '/ai/oeasy_work/mtcnn_1124_1206/mtcnn_tf_caffe/mtcnn_tf/t1112.jpg'\n",
    "#                      base_folder = 'mtcnn_1124_1206'\n",
    "#                      save_folder = 'save'\n",
    "#                 then:\n",
    "#                      save_path = '/ai/oeasy_work/save/mtcnn_tf_caffe/mtcnn_tf/t1112.jpg'\n",
    "#          else:\n",
    "#             Replace the original file\n",
    "        if margin_ratio >1.0:\n",
    "            margin_ratio =1.0\n",
    "\n",
    "        file_list_num = len(file_list)\n",
    "        print file_list_num\n",
    "        print type(file_list_num)\n",
    "        for cur_index in xrange(file_list_num):\n",
    "            print cur_index\n",
    "            print type(cur_index)\n",
    "            print type(path_list)\n",
    "            c_path = path_list[cur_index]\n",
    "            c_file = file_list[cur_index]\n",
    "            src_file = os.path.join(c_path, c_file)\n",
    "            try:\n",
    "                image_data = cv2.imread(filename)\n",
    "                img_size = image_data.shape\n",
    "                bounding_boxes, points = self.m_mtcnn.detect_face_v3(image_data, intput_type = 1)\n",
    "                \n",
    "                # There is only one person's face\n",
    "                if type(bounding_boxes) is np.ndarray and bounding_boxes.shape[0] == 1:\n",
    "                    c_rect = bounding_boxes[0]\n",
    "                    if margin_ratio >0:\n",
    "                        rect_w = int(c_rect[3]) - int(c_rect[1])\n",
    "                        rect_h = int(c_rect[2]) - int(c_rect[0])\n",
    "                        margin_w = rect_w * margin_ratio\n",
    "                        margin_h = rect_h * margin_ratio\n",
    "                    else:\n",
    "                        margin_w = margin_value\n",
    "                        margin_h = margin_value\n",
    "                    \n",
    "                    rect_margined = np.zeros(4, dtype=np.int32)\n",
    "                    rect_margined[0] = np.maximum(c_rect[0] - margin_w, 0)\n",
    "                    rect_margined[1] = np.maximum(c_rect[1] - margin_h, 0)\n",
    "                    rect_margined[2] = np.minimum(c_rect[2] + margin_w, img_size[1])\n",
    "                    rect_margined[3] = np.minimum(c_rect[3] + margin_h, img_size[0])\n",
    "                    margined_data = image_data[rect_margined[1]:rect_margined[3],rect_margined[0]:rect_margined[2]]\n",
    "                    dst_img = self.batch_processing.resize_image_data(margined_data, resize_type, resize_ratio, controled_size)\n",
    "                    if base_folder != '' and save_folder != '':\n",
    "                        c_save_folder = c_path.replace(base_folder, save_folder, 1)\n",
    "                        if not os.path.exists(c_save_folder):\n",
    "                            os.makedirs(c_save_folder)\n",
    "                    else:\n",
    "                        c_save_folder = c_path\n",
    "                    dst_file = os.path.join(c_save_folder, c_file)\n",
    "                    if not cv2.imwrite(dst_file, dst_img):\n",
    "                        print ('no_permission : ' + src_file)\n",
    "                else:\n",
    "                    os.remove(filename)\n",
    "            except:\n",
    "                print 'zzz'\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "c_mtcnn = mtcnn_batchprocess()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [],
   "source": [
    "path_list, file_list = c_mtcnn.get_file_list(file_path = '/ai/oeasy_work/mtcnn_1124_1206/mtcnn_tf_caffe/mtcnn_tf/10')\n",
    "print type(path_list)\n",
    "# c_mtcnn.mtcnn_process(path_list = path_list, file_list = file_list,margin_ratio = 0.143,resize_type = 2, controled_size = 256) "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "1.0/7\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 2",
   "language": "python",
   "name": "python2"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.12"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
